package what.basic.swingcommon;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class swingtransferhandler {
	private JFrame mf;
	private JPanel jp;
	private JLabel lbl;
	private JTextField txt;
	private JColorChooser cc;
	private JMenuBar mb=new JMenuBar();
	private JMenu menu=new JMenu("Menu");
	private JMenuItem mi=new JMenuItem("Handle Foreground");
	private TransferHandler t1=new TransferHandler("text");
	private TransferHandler t2=new TransferHandler("foreground");
	public swingtransferhandler(){
		mf=new JFrame();
		jp=new JPanel(new BorderLayout());
		lbl=new JLabel("label");
		lbl.setTransferHandler(t1);
		mi.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(lbl.getTransferHandler().equals(t1)){
					swingtransferhandler.this.mi.setText("Handle Text");
					lbl.setTransferHandler(t2);
				}
				else{
					swingtransferhandler.this.mi.setText("Handle forground");
					lbl.setTransferHandler(t1);
				}
			}
		});
		menu.add(mi);
		menu.setTransferHandler(t1);
		mb.add(menu);
		mf.setJMenuBar(mb);
		lbl.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				JComponent c=(JComponent)e.getSource();
				TransferHandler handler=c.getTransferHandler();
				handler.exportAsDrag(c, e, TransferHandler.COPY);
				System.out.println(c.toString()+"\n"+e.toString());
			}
		});
		mi.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent e){
				JComponent c=(JComponent)e.getSource();
				TransferHandler handler=c.getTransferHandler();
				handler.exportAsDrag(c, e, TransferHandler.COPY);
			}
		});
		mi.setTransferHandler(t1);
		txt=new JTextField(20);
		txt.setDragEnabled(true);
		cc=new JColorChooser();
		jp.add(lbl,BorderLayout.PAGE_START);
		jp.add(txt,BorderLayout.PAGE_END);
		jp.add(cc,BorderLayout.WEST);
		mf.getContentPane().add(jp);
		mf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mf.pack();
		mf.setLocationRelativeTo(null);
		mf.setVisible(true);
	}
	public static void main(String[] args) {
		new swingtransferhandler();
	}

}
